<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//购物车控制器
class IndentController extends CommonController{

	//构造函数
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
	}
	//购物车页面展示
	public function index(){		
			//取得session里的商品信息分配到页面
			$goods = isset($_SESSION['cart']['goods']) ? $_SESSION['cart']['goods']:array();
			View::with('goods',$goods);
			//分配商品总价
			$totals = isset($_SESSION['cart']['total']) ? $_SESSION['cart']['total']:0;
			View::with('totals',$totals);
			View::make();
	}
	
	//异步删除购物车的商品
	public function del(){
	   //接收异步发过来的数据
	   //删除$_SESSION中购物车里相应的数据
	    $data = array(
	   		'sid'  => Q('post.sid',''),
	   		//把购物数修改成0之后框架自动删除相应的购物车数据
	   		'num'  =>0,
	   );
	   Cart::update($data);
	}
	//异步修改购物车的商品
	public function edit(){
		//只需传入购物数量和货品唯一sid调用框架里的update方法就可修改
	   $data = array(
	   		'sid'  => Q('post.sid',''),
	   		'num'  => Q('post.num',0,'intval'),
	   );
	   Cart::update($data);
	}
	//异步清空购物车
	public function clear(){
	    if(Q('post.')){
	    	//将$_SESSION['cart']['goods']变为空数组即可，直接删除会报错
	    	$_SESSION['cart']['goods']=array();
			$_SESSION['cart']['total_rows'] = 0;
			$_SESSION['cart']['total'] = 0;
	    }
	}
	
	
	
	
	
	
	
	
	
}	
	
?>
		